#pragma once

#include "Entity.h"

class Boss : public Entity{
private:
	bool m_ai;
	bool m_checkBoundariesY;
	bool m_goUp;
	int m_screenWidth;
	int m_screenHeight;

	LPCWSTR m_bulletFileName;

	void AI(float a_time){
		static float t_timer = 0.0f;
		static float t_shootTimer = 0.0f;
		t_timer += a_time;
		t_shootTimer += a_time;
		if(t_timer >= 1.0f){
			m_speed += 10;
			t_timer = 0.0f;
		}
		if(t_shootTimer >= 0.5f){
			if(rand()%2 == 0){
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y);
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y+40);
			}else{
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y-(m_PhysicObject.m_height*0.25f)-40);
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y-(m_PhysicObject.m_height*0.25f)+40);
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y+(m_PhysicObject.m_height*0.25f)-40);
				Shoot(m_PhysicObject.m_position.x-(m_PhysicObject.m_width*0.5f), m_PhysicObject.m_position.y+(m_PhysicObject.m_height*0.25f)+40);
			}
			t_shootTimer = 0.0f;
		}
		if(m_checkBoundariesY){
			if((m_PhysicObject.m_position.y-(m_PhysicObject.m_height*0.5f)) < 0){
				m_goUp = false;
			}
			if((m_PhysicObject.m_position.y+(m_PhysicObject.m_height*0.5f)) > m_screenHeight){
				m_goUp = true;
			}
			if(m_goUp){
				if(m_PhysicObject.m_velocity.y > 0.0f){
					m_PhysicObject.m_velocity.y *= -1;
				}
			}else{
				if(m_PhysicObject.m_velocity.y < 0.0f){
					m_PhysicObject.m_velocity.y *= -1;
				}
			}
		}
	};

	void Shoot(float a_x, float a_y){
		Bullet *t_Bullet = new Bullet;
		t_Bullet->Initialize(m_bulletFileName);
		t_Bullet->Set_Data(V2DF(a_x, a_y),V2DF(-100.0f, 0.0f),1.0f,0.0f,400.0f);
		t_Bullet->m_PhysicObject.m_physic_object_type = PHYSIC_OBJECT_TYPE_SQUARE;
		t_Bullet->m_PhysicObject.m_height = 20.0f;
		t_Bullet->m_PhysicObject.m_width = 90.0f;
		m_BulletList.Insert_ToEnd(t_Bullet);
		m_PhysicSystem->Create((&t_Bullet->m_PhysicObject));	
	};

public:
	Boss(){
		m_ai = false;
		m_checkBoundariesY = false;
		m_goUp = false;
	};
	~Boss(){};

	void SET_AI(bool a_bool){
		if(a_bool){
			if(!m_ai){
				m_ai = true;
				m_checkBoundariesY = true;
				m_PhysicObject.m_velocity.set(0.0f, 0.0f);
				m_goUp = true;
				m_PhysicObject.m_velocity.set(0.0f, 1.0f);
			}

		}else{
			m_ai = false;
		}
		Entity::update();
	}

	void initialize(LPCWSTR fileName, LPCWSTR bulletFileName, PhysicSystem *a_PhysicSystem, int a_screenWidth, int a_screenHeight){
		Entity::initialize(fileName, bulletFileName, a_PhysicSystem);

		m_bulletFileName = bulletFileName;

		m_PhysicSystem = a_PhysicSystem;
		m_screenWidth = a_screenWidth;
		m_screenHeight = a_screenHeight;
	};
	void update(float dt){
		Entity::update();

		if(m_durability <= 0){
			m_bDead = true;
		}
		if(!m_bDead){

			if(m_ai){
				AI(dt);
			}

			for(int i = 0; i < m_BulletList.Get_Size(); i++){
				m_BulletList.Get_Data(i)->Update();
				if(!onScreen(m_BulletList.Get_Data(i)->m_PhysicObject) ) {
					m_PhysicSystem->Destroy(&m_BulletList.Get_Data(i)->m_PhysicObject);
					m_BulletList.Get_Data(i)->Release();
					m_BulletList.Remove_FromIndex(i);
				}
			}
		}

		m_PhysicObject.m_velocity.normalize();
		m_PhysicObject.m_velocity.multiply(m_speed);
		if(m_speed > 1000){
			m_speed = 1000;;
		}
	};
	void render(){
		Entity::render();

		for(int i = 0; i < m_BulletList.Get_Size(); i++){
			if(m_BulletList.Get_Data(i)) {
				m_BulletList.Get_Data(i)->Draw();
			}
		}
	};
	void release(){
		Entity::release();
	};
	bool alive() {
		if(m_durability <= 0) {
			return false;
		}
		else {
			return true;
		}
	}
};